<?php
namespace Nawa\MedicalStudy\MedicalCenter;
/**
 * Medical Center (of a Medical Study) Partners.
 *
 * It represant a Patient of the medical center or a customer
 *
 * @author Xavier VILAIN
 * @package Nawa\MedicalStudy\MedicalCenter
 *
 * @property int	$msid			Id of the MS
 * @property int	$mcid			Id of the MC
 * @property int 	$customerid 	Id of Customer Partner
 * @property string $role 			Partner Role
 * @property datetime $udate 		Last Change
 * @property int $uname 			last author
 */
class Partner extends \Fox\Object {

	/**
	 * Link to Medical Study
	 */
	const LINK_MEDICAL_STUDY = 'medicalStudy';

	/**
	 * Link to Medical Center
	 */
	const LINK_MEDICAL_CENTER = 'medicalCenter';

	/**
	 * Link to Medical Center
	 */
	const LINK_MEDICAL_CENTER_PARTNER = 'medicalCenterPartner';
	
	/**
	 * Link to Medical Studies Partners
	 */
	const LINK_MEDICAL_STUDIES_PARTNER = 'medicalStudyPartners';

	/**
	 * Link to Medical Studies Partners
	 */
	const LINK_MEDICAL_STUDIES_MEDICAL_CENTERS = 'medicalCenters';
	
	/**
	 *
	 * Link to Customer
	 * @var string
	 */
	const LINK_CUSTOMER = 'customer';

	/**
	 * Standard Role
	 */
	const ROLE_STANDARD = 'cust';

	/**
	 *
	 * Get id of the partner according to the parnter type
	 */
	public function partnerid()
	{
		return $this->customerid;
	}

	/**
	 * Corresponding Partner object (not the link)
	 * @return \Nawa\Customer
	 */
	public function partner() {
		try {
			$partner = \Fox\Dao::load(\Nawa\Customer::className(),$this->customerid);
		} catch(\Fox\Object\Exception\NotExist $e) {
			// Should not be possible
			$partner = new \Nawa\Customer();
		}
		return $partner;
	}
	
	/**
	 * Retrieve all data from ms-mc link corresponding mc label
	 * @return array
	 */
	public function medicalCentersData(array $fragments = null) {
		$handler = \Fox\Dao::infos(self::className())->handle();
		$query = $handler->createQueryBuilder();
		$query->select('*')
				->from('ms_mc_partners','t')
				->innerJoin('t','medicalcenters','mc','t.mcid = mc.mcid');
		$fragments[] = new \Fox\Dao\SqlFragment('msid = ? AND customerid = ?',array($this->msid,$this->customerid));
		$fragment = \Fox\Dao\SqlFragment::combine($fragments);
		return $fragment->pushIn($query)->execute()->fetchAll(\PDO::FETCH_ASSOC);
	}
	
}
\Fox\Dao::infos(Partner::className(),new \Fox\Object\Info('ms_mc_partners', \Fox\Databases::DB_APP));

\Nawa\Customer::className();

// Link to Medical Study Partners
/*
 \Fox\Object\Links::add(\Fox\Dao::link(MedicalStudy\Customizing::className(), MedicalStudy\Customizing::LINK_MEDICAL_STUDY)
 ->to(MedicalStudy::className(), MedicalStudy::LINK_CUSTOMIZING)
 ->on('msid')
 ->oneToOne()
 ->versionFrom());
 */